package com.zw.knowlege.service;

import com.alibaba.fastjson.JSONObject;
import com.zw.knowlege.common.StaticValue;
import com.zw.knowlege.pojo.CollectRecordPojo;
import com.zw.knowlege.pojo.ExperiencePojo;
import com.zw.knowlege.util.CommonUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class ParamParseService {
    /**
     * 统一解析前端传来的参数
     * @param paramJson
     * @param returnClass
     * @param fieldName
     * @return
     */
    public Object parseParam(JSONObject paramJson, Class returnClass, String[] fieldName){
        if(ExperiencePojo.class.isAssignableFrom(returnClass)){
            return parseExperiencePojo(paramJson);
        }else if(CollectRecordPojo.class.isAssignableFrom(returnClass)){
            return parseCollectRecordPojo(paramJson);
        }else if(Map.class.isAssignableFrom(returnClass)){
            return paramMap(paramJson, fieldName);
        }
        return null;
    }
    private Map<String, Object> paramMap(JSONObject paramJson, String[] fieldName){
        Map<String, Object> paramMap = new HashMap<>();
        Arrays.stream(fieldName).forEach(fieldname -> {
            if(paramJson.containsKey(fieldname) && !CommonUtils.nullOrEmpty(paramJson.getString(fieldname))){
                paramMap.put(fieldname, paramJson.getString(fieldname));
            }
        });
        return paramMap;
    }

    private CollectRecordPojo parseCollectRecordPojo(JSONObject paramJson){
        CollectRecordPojo collectRecordPojo = new CollectRecordPojo();
        collectRecordPojo.setRecordid(paramJson.getString(StaticValue.EXPERIENCE_ID))
                         .setUserid(paramJson.getString(StaticValue.EXPERIENCE_USERINFO_USERID))
                         .setCollecttime(new Timestamp(System.currentTimeMillis()))
                         .setType(paramJson.getString(StaticValue.RECORDTYPE))
                         .setAction(paramJson.getString(StaticValue.EXPERIENCE_ACTION));
        return collectRecordPojo;
    }

    private ExperiencePojo parseExperiencePojo(JSONObject paramJson){
        ExperiencePojo experiencePojo = new ExperiencePojo();
        return experiencePojo.setExperiencetitle(paramJson.getString(StaticValue.EXPERIENCE_TITLE))
                .parseUserinfo(paramJson.getJSONObject(StaticValue.EXPERIENCE_USERINFO))
                .setClassinfo(paramJson.getJSONArray(StaticValue.EXPERIENCE_CLASSINFO))
                .setDepttype(paramJson.getJSONArray(StaticValue.EXPERIENCE_DEPTTYPE))
                .setDescription(paramJson.getOrDefault(StaticValue.EXPERIENCE_DESCRIPTION, StaticValue.NULL).toString())
                .setLegalrefname(paramJson.getOrDefault(StaticValue.EXPERIENCE_LEGALREFNAME, StaticValue.NULL).toString())
                .setLegalrefurl(paramJson.getOrDefault(StaticValue.EXPERIENCE_LEGALREFURL, StaticValue.NULL).toString())
                .setRelatecaseid(paramJson.containsKey(StaticValue.EXPERIENCE_RELATECASEID) ? paramJson.getJSONArray(StaticValue.EXPERIENCE_RELATECASEID) : null)
                .setHandmethod(paramJson.getOrDefault(StaticValue.EXPERIENCE_HANDMETHOD, StaticValue.NULL).toString())
                .setAttention(paramJson.containsKey(StaticValue.EXPERIENCE_ATTENTION) ? paramJson.getJSONArray(StaticValue.EXPERIENCE_ATTENTION) : null)
                .setRemark(paramJson.getOrDefault(StaticValue.EXPERIENCE_REMARK, StaticValue.NULL).toString())
                .setAction(paramJson.getOrDefault(StaticValue.EXPERIENCE_ACTION, StaticValue.NULL).toString())
                .setCreatetime()
                .setExperienceid(paramJson.getOrDefault(StaticValue.EXPERIENCE_ID, StaticValue.NULL).toString())
                .setStatus()
                .setIsrelease();
    }
}
